package com.mj.mafeed.adapter;

import java.util.HashMap;
import java.util.List;

import com.mj.mafeed.R;
import com.mj.mafeed.bean.ReceiverJSON;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 选择收货地址
 * @author zhaominglei
 * @date 2014-12-17
 * 
 */
public class ReceiverSelectAdapter extends BaseAdapter {
	private List<ReceiverJSON> receivers;
	private Context context;
	@SuppressLint("UseSparseArrays")
	private HashMap<Long, Boolean> isSelected = new HashMap<Long, Boolean>();
	
	
	public ReceiverSelectAdapter(Context context, List<ReceiverJSON> receivers) {
		super();
		this.context = context;
		this.receivers = receivers;
	}

	public List<ReceiverJSON> getReceivers() {
		return receivers;
	}

	public void setReceivers(List<ReceiverJSON> receivers) {
		this.receivers.addAll(receivers);
		initDate();
	}

	private void initDate() {  
        for (ReceiverJSON receiverJSON : receivers) {
        	getIsSelected().put(receiverJSON.getReceiverId(), false);
		}
    }
	
	@Override
	public int getCount() {
		return receivers.size();
	}

	@Override
	public Object getItem(int position) {
		return receivers.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout relativeLayout;
		if (convertView != null) {
			relativeLayout = (RelativeLayout) convertView;
		} else {
			relativeLayout = (RelativeLayout) View.inflate(context,
					R.layout.receiverselect_item, null);
		}
		final ReceiverJSON receiverJSON = receivers.get(position);
		relativeLayout.setTag(receiverJSON.getReceiverId());
		((TextView) relativeLayout.findViewById(R.id.receiverselect_item_template_areaname))
				.setText(receiverJSON.getAreaName());
		((TextView) relativeLayout.findViewById(R.id.receiverselect_item_template_consignee))
				.setText(receiverJSON.getConsignee());
		((TextView) relativeLayout.findViewById(R.id.receiverselect_item_template_phone))
				.setText(receiverJSON.getPhone());
		((TextView) relativeLayout.findViewById(R.id.receiverselect_item_template_address))
				.setText(receiverJSON.getAddress());
		((TextView) relativeLayout.findViewById(R.id.receiverselect_item_template_zipCode))
				.setText(receiverJSON.getZipCode());
		ImageView selectView = (ImageView) relativeLayout.findViewById(R.id.receiverselect_item_select); //选择
//		if (getIsSelected() == null) {
//			if (receiverJSON != null && receiverJSON.getIsDefault() != null && receiverJSON.getIsDefault()) {
//				selectView.setVisibility(View.VISIBLE);
//			} else {
//				selectView.setVisibility(View.INVISIBLE);
//			}
//		} else {
			if (getIsSelected().get(receiverJSON.getReceiverId())) {
				selectView.setVisibility(View.VISIBLE);
			} else {
				selectView.setVisibility(View.INVISIBLE);
			}
//		}
		
		return relativeLayout;
	}

	public HashMap<Long, Boolean> getIsSelected() {
		return isSelected;
	}

	public void setIsSelected(HashMap<Long, Boolean> isSelected) {
		this.isSelected = isSelected;
	}

}
